find_package(Qt${QT_VERSION_MAJOR} QUIET COMPONENTS SerialBus)

macro(remove_all_x_modbus_files)
  file(GLOB_RECURSE MODBUS_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/src/x/modbus/*.*")
  foreach(file ${MODBUS_SOURCE})
    list(REMOVE_ITEM X_SOURCES ${file})
    message(STATUS "[Modbus]Remove file: ${file}")
  endforeach(file ${MODBUS_SOURCE})
endmacro()

macro(remove_all_x_canbus_files)
  file(GLOB_RECURSE CANBUS_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/src/x/canbus/*.*")
  foreach(file ${CANBUS_SOURCE})
    list(REMOVE_ITEM X_SOURCES ${file})
    message(STATUS "[Canbus]Remove file: ${file}")
  endforeach(file ${CANBUS_SOURCE})
endmacro()

if(Qt${QT_VERSION_MAJOR}SerialBus_FOUND)
  add_compile_definitions(X_ENABLE_SERIALBUS)
  list(APPEND X_LIBS Qt${QT_VERSION_MAJOR}::SerialBus)

  if(QT_VERSION VERSION_LESS "6.8.0")
    add_compile_definitions(X_ENABLE_X_MODBUS=0)
    remove_all_x_modbus_files()

    add_compile_definitions(X_ENABLE_X_CNABUS=0)
    remove_all_x_canbus_files()
  else()
    add_compile_definitions(X_ENABLE_X_MODBUS=0)
    add_compile_definitions(X_ENABLE_X_CNABUS=0)
  endif()
else()
  message(STATUS "SerialBus module is disable, SerialBus files will be removed.")
  # Remove modbus files
  file(GLOB_RECURSE MODBUS_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/src/tools/modbus/*.*")
  foreach(file ${MODBUS_SOURCE})
    list(REMOVE_ITEM X_SOURCES ${file})
    message(STATUS "[Modbus]Remove file: ${file}")
  endforeach(file ${MODBUS_SOURCE})

  add_compile_definitions(X_ENABLE_X_MODBUS=0)
  remove_all_x_modbus_files()

  # Remove canbus files
  file(GLOB_RECURSE CANBUS_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/src/tools/canbus/*.*")
  foreach(file ${CANBUS_SOURCE})
    list(REMOVE_ITEM X_SOURCES ${file})
    message(STATUS "[Canbus]Remove file: ${file}")
  endforeach(file ${CANBUS_SOURCE})

  add_compile_definitions(X_ENABLE_X_CNABUS=0)
  remove_all_x_canbus_files()
endif()
